package web;

import pojo.User;
import service.UserService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.IOException;

@WebServlet("/register")
public class RegisterServlet extends HttpServlet {
    UserService userService =new UserService();
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    String username = request.getParameter("username");
    String password = request.getParameter("password");
    String checkCode = request.getParameter("checkCode");
        User user = new User();
    user.setUsername(username);
    user.setPassword(password);
        HttpSession session = request.getSession();
        String s = (String) session.getAttribute("s");
        if (!s.equalsIgnoreCase(checkCode))
        {
            request.setAttribute("msg","验证码错误，请重新输入！！！");
            request.getRequestDispatcher("/register.jsp").forward(request,response);
            return;
        }
        boolean register = userService.register(user);
        if(register)
        {
            request.setAttribute("msg","注册成功，请登录！！！");
            request.getRequestDispatcher("/login.jsp").forward(request,response);
        }
        else
        {

            request.setAttribute("msg","用户名已存在！！！");
            request.getRequestDispatcher("/register.jsp").forward(request,response);

        }
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
    }
}
